package com.example.demo.screenshare;

import java.awt.Frame;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;

/*
 * ly  2014-11-20
 * 该类用于接收教师端的屏幕信息，不包括鼠标，待优化
 */
public  class ReceiveImages extends  Thread{
    public BorderInit frame ;
    public Socket socket;
    public String IP;

    public static void main(String[] args){
        new ReceiveImages(new BorderInit(), "127.0.0.1").start();
    }
    public ReceiveImages(BorderInit frame,String IP)
    {
        this.frame = frame;
        this.IP=IP;
    }

    public void run() {
        while(frame.getFlag()){
            try {
                socket = new Socket(IP,8000);
                DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
                ZipInputStream imgZip = new ZipInputStream(ImgInput);

                imgZip.getNextEntry();             //到Zip文件流的开始处
                Image img = ImageIO.read(imgZip);  //按照字节读取Zip图片流里面的图片
                ImageIcon imageIcon = new ImageIcon(img);
                // 图片宽高比
                double img_rate = 1.0*imageIcon.getIconWidth()/ imageIcon.getIconHeight();
                // 窗口宽高比
                double win_rate = 1.0*frame.getWidth()/ frame.getHeight();
                int width;
                int height;
                int poi_x = 0;
                int poi_y = 0;
                if(img_rate > win_rate) {
                    // 图片比窗口扁
                    width = frame.getWidth();
                    height = (int) (width/img_rate);
                    poi_y = (frame.getHeight() - height)/2;
                    width -= 10;
                }else {
                    height = frame.getHeight();
                    width = (int) (height * img_rate);
                    poi_x = (frame.getWidth() - width)/2;
                    height -=10;
                }

                // 计算图片自适应大小
                System.out.println("图片原始尺寸： w="+imageIcon.getIconWidth() + "\th:"+imageIcon.getIconHeight() +"\t rate:"+img_rate);
                System.out.println("屏幕尺寸： w="+frame.getWidth() + "\th:"+frame.getHeight() +"\t rate:"+win_rate);
                System.out.println("图片尺寸： w="+width + "\th:"+height +"\t rate:"+(1.0*width/height));
                imageIcon.setImage(imageIcon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));//图片自适应窗口大小
                frame.jlbImg.setBounds(poi_x, poi_y, width ,height);//设置标签位置大小，记得大小要和窗口一样大
                //frame.getLayeredPane().add(frame.jlbImg, Integer.valueOf(Integer.MIN_VALUE));//标签添加到层面板
                frame.jlbImg.setIcon(imageIcon);
                System.out.println("连接第"+(System.currentTimeMillis()/1000)%24%60+"秒");
                frame.validate();
                TimeUnit.MILLISECONDS.sleep(50);// 接收图片间隔时间
                imgZip.close();

            } catch (IOException | InterruptedException e) {
                System.out.println("连接断开");
            }finally{
                try {
                    socket.close();
                } catch (IOException e) {}
            }
        }
    }
}

//Client端窗口辅助类，专门用来显示从教师端收到的屏幕信息
class BorderInit extends JFrame
{
    private static final long serialVersionUID = 1L;
    public JLabel jlbImg;
    private boolean flag;
    public boolean getFlag(){
        return this.flag;
    }
    public BorderInit()
    {
        this.flag=true;
        this.jlbImg = new JLabel();
        this.setTitle("远程监控--IP:"  + "--主题:" );
        this.setSize(600, 400);
        //this.setUndecorated(true);  //全屏显示，测试时最好注释掉
        //this.setAlwaysOnTop(true);  //显示窗口始终在最前面
        this.add(jlbImg);
        JPanel imPanel=(JPanel) this.getContentPane();//注意内容面板必须强转为JPanel才可以实现下面的设置透明
        imPanel.setOpaque(false);//将内容面板设为透明
        this.setLocationRelativeTo(null);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.validate();
        this.getContentPane().setLayout(null);//无布局，记得下面添加控件时要设置它们位置和大小


        //窗口关闭事件
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                flag=false;
                BorderInit.this.dispose();
                System.out.println("窗体关闭");
                System.gc();    //垃圾回收
            }
        });
    }
}
